﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Shove.Database;

public partial class SystemManage_LoginLog : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        isAtFramePageLogin = true;                              //是框架页
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url        
        RequestCompetences = Competences.BuildCompetencesList(Competences.Log);//页面权限

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        string strCmd = "select * from V_LoginLog";
        int QueryTime = Shove._Convert.StrToInt(ddlTime.SelectedValue, -1);

        if (QueryTime > 0)
        {
            strCmd += " where Datediff(day, [OperatorDate], GetDate()) <= " + QueryTime.ToString();
        }

        if (tbUserName.Text.Trim() != "")
        {
            strCmd += (strCmd.IndexOf("where") > 0 ? " and" : " where") + " [CustomerNO] = '" + tbUserName.Text.Trim() + "'";
        }

        strCmd += " order by [OperatorDate]";

        DataTable dt = MSSQL.Select(strCmd);

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        PublicFunction.DataGridBindData(g, dt, gPager);
        
        btnClear.Enabled = (dt.Rows.Count > 0);
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            if (e.Item.Cells[7].Text == "0")
            {
                e.Item.Cells[7].Text = "正常登录";
            }
            else if (e.Item.Cells[7].Text == "1")
            {
                e.Item.Cells[7].Text = "<font color='red'>密码错误</font>";
            }
            else if (e.Item.Cells[7].Text == "2")
            {
                e.Item.Cells[7].Text = "<font color='blue'>冻结登录</font>";
            }
            else if (e.Item.Cells[7].Text == "3")
            {
                e.Item.Cells[7].Text = "取回密码";
            }
            else if (e.Item.Cells[7].Text == "4")
            {
                e.Item.Cells[7].Text = "清除日志";
            }
            else
            {
                e.Item.Cells[7].Text = "其它";
            }
        }
    }

    protected void btnGo_Click(object sender, System.EventArgs e)
    {
        BindData();
    }

    protected void btnClear_Click(object sender, System.EventArgs e)
    {
        string strCmd = "delete from tbl_SystemLog";
        int QueryTime = Shove._Convert.StrToInt(ddlTime.SelectedValue, -1);

        if (QueryTime > 0)
        {
            strCmd += " where Datediff(day, OperatorDate, GetDate()) <= " + QueryTime.ToString();
        }

        if (tbUserName.Text.Trim() != "")
        {
            Customer c = new Customer(_Site.ID);
            c.CustomerNO = tbUserName.Text.Trim();
            string sReturn = "";
            c.GetCustomerInformationByNO(ref sReturn);

            if (sReturn != "")
            {
                Shove._Web.JavaScript.Alert(this.Page, sReturn);

                return;
            }
            strCmd += (strCmd.IndexOf("where") > 0 ? " and" : " where") + "  CustomerID = " + c.ID;
        }

        if (MSSQL.ExecuteNonQuery(strCmd) < 0)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        BindData();
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }
}